package com.newer.controller.sys;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newer.pojo.sys.Organization;
import com.newer.pojo.sys.Staff;
import com.newer.service.sys.iter.StaffService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.MsgUtil;

@Controller
@RequestMapping("staff")
public class StaffController {

	// 自动注入
	@Autowired
	private StaffService staffService;

	/**
	 * 删除取派员，假删除，实际上是把deltag改成1
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	public @ResponseBody MsgUtil delete(Staff staff) {
		try {
			staff.setDeltag("1");
			if (staffService.updateByPrimaryKeySelective(staff) > 0) {
				MsgUtil.result(true, "删除成功");
			}
		} catch (Exception e) {
			MsgUtil.result(true, "删除失败");
		}
		return MsgUtil.result(true, "删除失败");
	}

	/**
	 * 批量删除
	 * 
	 * @param Ids
	 * @return
	 */
	@RequestMapping("batchdelete/{ids}")
	public @ResponseBody MsgUtil toDeleteIds(@PathVariable("ids") String Ids) {
		try {
			staffService.deleteStaffByIds(Ids);
			return MsgUtil.result(true, "删除成功");
		} catch (Exception e) {
			return MsgUtil.result(false, "删除失败");
		}
	}

	/**
	 * 跳转添加页面并查出部门信息
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("addPage")
	public String addPage(HttpServletRequest request, Model model) {
		List<Organization> organizations = staffService.selectAllOrganization();
		model.addAttribute("organizations", organizations);
		return "sys/staff/staff_add";

	}

	/**
	 * 添加取派员
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping("add")
	public @ResponseBody MsgUtil add(Staff staff) {
		try {
			// 设置当前系统时间
			staff.setCreateDate(new Date());
			if (staffService.insert(staff) > 0) {
				MsgUtil.result(true, "添加成功");
			}
		} catch (Exception e) {
			MsgUtil.result(true, "添加失败");
		}
		return MsgUtil.result(true, "添加失败");
	}

	/**
	 * 查询所有取派员
	 * 
	 * @param dataTable
	 * @param conditions
	 * @return
	 */
	@RequestMapping("staffAll")
	public @ResponseBody BootstrapDataTable staffAll(BootstrapDataTable dataTable, Conditions conditions) {
		return staffService.selectStaffByExample(dataTable, conditions);
	}

	/**
	 * 根据id查询，返回编辑页面
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("selectStaffById/{id}")
	public String selectStaffById(@PathVariable("id") Integer id, Model model) {
		Staff staff = staffService.selectByPrimaryKey(id);
		List<Organization> organizations = staffService.selectAllOrganization();
		model.addAttribute("organizations", organizations);
		model.addAttribute("staff", staff);
		return "sys/staff/staff_edit";
	}

	/**
	 * 修改取派员信息
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping("edit")
	public @ResponseBody MsgUtil edit(Staff staff) {
		try {
			staffService.updateByPrimaryKeySelective(staff);
			return MsgUtil.result(true, "修改成功");
		} catch (Exception e) {
			return MsgUtil.result(false, "修改失败");
		}
	}

	/**
	 * 查询所有部门
	 * 
	 * @return
	 */
	@RequestMapping("getOrganization")
	public @ResponseBody List<Organization> getOrganization() {
		return staffService.selectAllOrganization();
	}
}
